package ModelLayer;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * Write a description of class DVDContainer here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class DVDContainer
{
    // instance variables - replace the example below with your own
    private ArrayList<DVD> dvdlist;
    private static DVDContainer instance; //Singleton

    /**
     * Singleton constructor
     */
    private DVDContainer()
    {      
        dvdlist = new ArrayList<DVD>();
    }

    //Singleton
    public static DVDContainer getInstance()
    {
        if(instance == null)
        {
            instance = new DVDContainer();
        }
        return instance;
    }

    //add DVD to ArrayList<Person>
    public void addDVD(DVD d)
    {
        dvdlist.add(d);
    }

    //find person in ArrayList<Person>
    public DVD findDVD(int no)
    {
        DVD dvd = null;
        int index = 0;
        int rememberIndex = 0;
        boolean found = false;

        while(index < dvdlist.size() && !found)
        {
            dvd = dvdlist.get(index);

            if(dvd.getID() == no)
            {
                found = true;
                rememberIndex = index;
            }
            index++;
        }
        if(found)
        {
            return dvdlist.get(rememberIndex);
        }
        else
        {
            return null;
        }
    }

    //update dvd in ArrayList<DVD>
    public void UpdateDVD(int no, String title, String publisher, String releasedate)
    {
        DVD dvd = null;
        int index =  0;
        int rememberIndex = 0;
        boolean found = false;

        while(index < dvdlist.size() && !found)
        {
            dvd = dvdlist.get(index);

            if(dvd.getID() == no)
            {
                found = true;
                rememberIndex = index;
            }
            index++;
        }
        if(found)
        {
            dvd = dvdlist.get(rememberIndex);
            dvd.setTitle(title);
            dvd.setPublisher(publisher);
            dvd.setReleasedate(releasedate);
            System.out.println("the DVD: "+title+" has been updated");
        }
        else
        {
            System.out.println("The DVD: "+title+" dosen't exist");
        }

    }

    public void deleteDVD(int no)
    {
        int index = 0;
        int rememberIndex = 0;
        boolean found = false;
        DVD dvd = null;

        while(index < dvdlist.size() && !found)
        {
            dvd = dvdlist.get(index);

            if(dvd.getID() == no)
            {
                found = true;
                rememberIndex = index;
            }
            index++;

        }
        if(found)
        {
            System.out.println("The DVD with id "+no+" has been removed.");
            dvdlist.remove(rememberIndex);
        }
        else
        {
            System.out.println("A DVD with id "+no+" dosen't exist");
        }

    }

    public void printDetailDVD()
    {

        if(dvdlist != null)
        {
            for(DVD filename : dvdlist)
            {
                filename.printDetailDVD();
            }
        }
        else
        {
            System.out.println("there is no dvd");
        }

    }

    public void printDeatilDVDandCopies()
    {
        if(dvdlist != null)
        {
            for(DVD filename : dvdlist)
            {
                filename.printAllCopy();
            }
        }
        else
        {
            System.out.println("there is no dvd");
        }
    }
        
    public int getSize()
    {
        return dvdlist.size();
    }
        
    public int getSerieNoCopy(int no)
    {
        DVD dvd = null;
        int index = 0;
        int rememberIndex = 0;
        boolean found = false;

        while(index < dvdlist.size() && !found)
        {
            dvd = dvdlist.get(index);

            if(dvd.getID() == no)
            {
                found = true;
                rememberIndex = index;
            }
            index++;
        }
        if(found)
        {
            return dvdlist.get(rememberIndex).getSerieNo();
        }
        else
        {
            return 0;
        }
    }
    
    
}
